import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(home: MyApp2()));
}

class MyApp2 extends StatefulWidget {
  const MyApp2({super.key});

  @override
  State<MyApp2> createState() {
    print('createState - 1. 创建');

    return _MyApp2State();
  }
}

class _MyApp2State extends State<MyApp2> {
  // 年龄状态
  int age = 18;

  @override
  void initState() {
    // 初始化状态
    print('initState 2. 初始化');
    age = 20;
    super.initState();
  }

  @override
  void didChangeDependencies() {
    // 依赖发生变化
    print('didChangeDependencies 3. 依赖改变');
    super.didChangeDependencies();
  }

  @override
  void didUpdateWidget(covariant MyApp2 oldWidget) {
    // 状态更新
    print('didUpdateWidget 执行了');
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    // 销毁
    print('dispose 执行了');
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print('build 4. 构建');
    return Scaffold(
      appBar: AppBar(
        title: const Text('有状态组件'),
        backgroundColor: Colors.purple,
      ),
      body: Center(
        child: Text(
          '我叫朱丽叶, 今年$age岁',
          style: const TextStyle(
              color: Color.fromARGB(255, 30, 16, 187), fontSize: 20.0),
        ),
      ),
      // 右下角悬浮按钮
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            age++;
          });
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}
